package com.joeyzh.base;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatDelegate;
import android.text.TextUtils;
import android.view.View;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Joey on 2018/10/30.
 * 用于基础的ViewModel类，用于处理View和model层的数据绑定
 *
 * @author Joey
 */

public abstract class IViewModel<Bean extends Object> {

    Bean entityBean;
    Map<String, View> viewMap = new HashMap<>();

    public IViewModel(Bean bean) {
        entityBean = bean;
    }

    public Map<String, View> getViewMap() {
        return viewMap;
    }

    public Bean getEntityBean() {
        return entityBean;
    }

    public void updateEntityBean(Bean bean) {
        this.entityBean = bean;
    }

    public <T extends View> IViewModel bindViewItem(String key, T view) {
        if (view == null || TextUtils.isEmpty(key)) {
            return this;
        }
        viewMap.put(key, view);
        return this;
    }

    public <T extends View> IViewModel bindViewItem(AppCompatDelegate context, String key, @IdRes int resId) {
        T view = context.findViewById(resId);
        if (view == null || TextUtils.isEmpty(key)) {
            return this;
        }
        return bindViewItem(key, view);
    }

    public <T extends View> IViewModel bindViewItem(View root, String key, @IdRes int resId) {
        T view = root.findViewById(resId);
        if (view == null || TextUtils.isEmpty(key)) {
            return this;
        }
        return bindViewItem(key, view);
    }

}
